package com.xiaotu.spider.douban.pageprocessor;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.xiaotu.common.model.ShortCommentsModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;
/***
 * 豆瓣电影短评最热500条数
 * @author Administrator
 *
 */
@Component("DouBanMovieSmallCommentPageProcessor")
@Scope("prototype")
public class DouBanMovieSmallCommentPageProcessor extends AUrlPageProcessor{
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> movieMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.TV_COMMENT:
                this.downLoadData(page,movieMap);
                break;
            case "getMovieId":
                this.addRequest(page,movieMap);
                break;
            default:
                break;
        }
		
	}
	//用于获取数据
	private void downLoadData(Page page,Map<String, Object> movieMap) {
		//获取需要爬去的数据
		//电影名称
		String movieName = movieMap.get("movieName").toString();
		//每条短评
		List<String> allComments = page.getHtml().xpath("//div[@class='comment-item']").all();
		
		List<ShortCommentsModel> shortCommentsModelList = new ArrayList<ShortCommentsModel>();
		
		for(String shortComment : allComments){
			
			Html commentHtml = new Html(shortComment);
			//内容
			String comment = commentHtml.xpath("//span[@class='short']/text()").get().replaceAll("[^(0-9a-zA-Z\u4e00-\u9fa5\\p{P}\r\n )]", "");;
			//评论时间
			String commentTime = commentHtml.xpath("//span[@class='comment-time ']/@title").get();
			//点赞数
			Integer thumbsUpNum = Integer.parseInt(commentHtml.xpath("//span[@class='votes']/text()").get());
			//踩数
			Integer thumbsDownNum = null;
			//再评论数
			Integer reComentsNum = null;
			//星级
			Integer score = this.getScore(commentHtml.xpath("//span[@class='comment-info']/span[2]/@class").get());
			//数据id
			String id = commentHtml.xpath("//div[@class='comment-item']/@data-cid").get();
			ShortCommentsModel shortCommentsModel = new ShortCommentsModel(
					UUIDUtils.getId(),movieName,"豆瓣",comment,commentTime,
					thumbsUpNum,thumbsDownNum,reComentsNum,score);
			shortCommentsModelList.add(shortCommentsModel);
			
		}
		page.putField(ShortCommentsModel.class.getName(), shortCommentsModelList);
	}

	//用于添加新的请求
	private void addRequest(Page page,Map<String, Object> movieMap) {
		String jsonStr = HttpUtils.httpGetJSONP(page.getRequest().getUrl());
		//当前电影名字
		String movieName = page.getRequest().getUrl().substring(page.getRequest().getUrl().indexOf("=")+1);
		movieMap.put("movieName", URLDecoder.decode(movieName));
		JSONArray jsonArr = JSONArray.parseArray(jsonStr);
		for(int i=0;i<jsonArr.size();i++){
			if(jsonArr.getJSONObject(i).getString("title").equals(URLDecoder.decode(movieName))){
				String movieId = jsonArr.getJSONObject(i).getString("id");
				if(StringUtils.isNotBlank(movieId)){
					for(int j=0;j<=480;j+=20){
						PageUtils.addTargetRequest(page,"https://movie.douban.com/subject/"+movieId+"/comments?start="+j+"&limit=20&sort=new_score&status=P",movieMap);
//						try {
//							Thread.sleep(3000);
//							PageUtils.addTargetRequest(page,"https://movie.douban.com/subject/"+movieId+"/comments?start="+j+"&limit=20&sort=new_score&status=P",movieMap);
//						} catch (InterruptedException e) {
//							e.printStackTrace();
//						}
					}
				}
				break;
			}
		}
		
		
	}

	@Override
	protected String[] getStartRequestUrls() {
		String[] urlArr = {
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("巨齿鲨"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("我不是药神"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("前任3：再见前任"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("影"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("战狼2"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("网络谜踪"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("西虹市首富"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("流浪地球"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("头号玩家"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("三块广告牌"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("疯狂的外星人"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("捉妖记2"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("幕后玩家"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("一出好戏"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("超时空同居"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("来电狂响"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("邪不压正"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("无间道"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("神奇动物在哪里"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("纯洁心灵·逐梦演艺圈"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("让子弹飞"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("无名之辈"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("动物世界"),
				"https://movie.douban.com/j/subject_suggest?q="+KeywordEncode.encode("叶问外传：张天志")
		};
		return urlArr;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_DOUBAN_COMMENT;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
		String dataType = "";
		if (url.contains("https://movie.douban.com/subject/")){
			dataType = Constants.PageDataType.TV_COMMENT;
				}
        else if (url.contains("https://movie.douban.com/j/subject_suggest?q=")){
        	dataType = "getMovieId";
        };
        return dataType;
	}
	
	//匹配星级
	private int getScore(String match){
		int score = 0;
		if(match.contains("10")){
			score = 2;
		}else if(match.contains("20")){
			score = 4;
		}else if(match.contains("30")){
			score = 6;
		}else if(match.contains("40")){
			score = 8;
		}else if(match.contains("50")){
			score = 10;
		}
		return score;
	}
	
}
